package com.bz.xtcx.manager.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;

import com.bz.xtcx.manager.entity.CmsFile;

public interface CmsFileMapper {

	@Insert("insert into `cms_file`(id, owner_type, owner_id, file_name, save_path, req_rel_url)"
			+ " values(#{id, jdbcType=VARCHAR}, #{ownerType, jdbcType=INTEGER}, #{ownerId, jdbcType=VARCHAR},"
			+ "#{fileName, jdbcType=VARCHAR},#{savePath, jdbcType=VARCHAR}, #{reqRelUrl, jdbcType=VARCHAR})")
	@SelectKey(before = true, keyProperty = "id", resultType = String.class, statementType = StatementType.STATEMENT, statement="select uuid()")
	int insert(CmsFile e);
	
	@Update("update `cms_file` set owner_id = #{ownerId, jdbcType=VARCHAR} where id = #{id, jdbcType=VARCHAR}")
	int updateOwnerId(CmsFile e);
	
	@Select("select * from `cms_file` where owner_id = #{ownerId}")
	@Results(
            id = "cmsFile",
            value = {
                @Result(id = true, property = "id", column = "id"),
                @Result(property = "ownerType", column = "owner_type"),
                @Result(property = "ownerId", column = "owner_id"),
                @Result(property = "fileName", column = "file_name"),
                @Result(property = "savePath", column = "save_path"),
                @Result(property = "reqRelUrl", column = "req_rel_url")
            }
    )
	List<CmsFile> fingByOwnerId(String ownerId);
	
	@Delete("delete from `cms_file` where id = #{id}")
	int del(String id);
	
	@Select("select * from `cms_file` where id = #{id}")
	@ResultMap("cmsFile")
	CmsFile findById(String id);
}
